import $http from "util/https/index.js"

const upFilesFn = (url) => {
	console.log("上传文件地址：", url)
	let this_ = this
	let fileObj = ""
	let aliFilePath = ""
	let sigend_url = ""
	let arrayBuffer = ""
	return new Promise((resolve, reject) => {
		plus.io.resolveLocalFileSystemURL(url, function(entry) {
				// 可通过entry对象操作test.html文件 
				entry.file(function(file) {
					console.log(file);
					fileObj = file;

					let params = {
						file_name: file.name,
						content_type: file.type
					}
					console.log(params)
					$http.get("api/doc/getUploadOssToken", params).then(res => {
						if (res.code == 0) {
							aliFilePath = res.data.path
							sigend_url = res.data.sigend_url

							console.log("getFile:" + JSON.stringify(fileObj));
							var fileReader = new plus.io.FileReader();
							fileReader.readAsDataURL(fileObj, "utf-8");
							fileReader.onloadend = function(evt) {
								let base64 = evt.target.result.split(',')[
									1]; // 获取base64字符串
								let arrayBuffer = uni.base64ToArrayBuffer(base64);
								console.log(arrayBuffer);
								console.log(sigend_url);

								uni.request({
									url: sigend_url, //仅为示例，并非真实接口地址。
									method: "PUT",
									data: arrayBuffer,
									header: {
										'Content-Type': fileObj
											.type //自定义请求头信息
									},
									success(res) {
										console.log(res)
										if (res.statusCode == 200) {
											let obj = {
												ststus: 0,
												url: aliFilePath
											}
											resolve(obj)
										} else {
											let obj = {
												ststus: -1,
												url: null,
												msg: "上传失败，请稍候再试"
											}
											reject(obj)
										}
									},
									fail(err) {
										console.log(err);
										let obj = {
											ststus: -1,
											url: null,
											msg: "上传失败，请稍候再试"
										}
										reject(obj)
									}
								})


							}

						} else {
							let obj = {
								ststus: -1,
								url: null,
								msg: "上传失败，请稍候再试"
							}
							reject(obj)							
						}

					})
				})
			},
			function(e) {
				console.log("Resolve file URL failed: " + e.message);
			})
	})

}


/*




// 							

// 								
							
					


*/
export {
	upFilesFn
}